/*
 * @Author: tkp
 * @Date: 2021-11-20 16:39:42
 */
// 导入axios
import axios from 'axios'
import loadEvents from './loading'
import store from '@/store/index'
import { setting } from './setting';
let loads = new loadEvents();
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
NProgress.configure({ showSpinner: false })
const service = axios.create({
    baseURL: setting(),
    // 超时时间 单位是ms，这里设置了10s的超时时间
    timeout: 10 * 1000
})
// 2.请求拦截器
service.interceptors.request.use(config => {
    loads.open()
    NProgress.start()
    if (store.state.user.token) {
        config.headers['jwt-token'] = store.state.user.token
        config.headers['urlfrom'] = 'authority'
    }
    return config
}, error => {
    loads.close()
    NProgress.done()
    Promise.reject(error)
})
// 3.响应拦截器
service.interceptors.response.use(response => {
    loads.close()
    NProgress.done()
    return response.data
}, error => {
    loads.close()
    NProgress.done()
    return Promise.reject(error)
})

//4.导入文件
export default service